package com.guyaning.deyson_ec.ec.main.sort.content;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;

import com.guyaning.deyson_core.core.delegates.DeysonDelegate;
import com.guyaning.deyson_core.core.net.RestClient;
import com.guyaning.deyson_core.core.net.callback.ISuccess;
import com.guyaning.deyson_ec.ec.R;
import com.guyaning.deyson_ec.ec.R2;

import java.util.List;

import butterknife.BindView;

/**
 * Created by Administrator on 2018/6/26.
 */

public class ContentDelegate extends DeysonDelegate {

    private static String ARG_CONTENT_ID = "CONTNENT_ID";

    @BindView(R2.id.rl_list_content)
    RecyclerView mRecyclerView = null;

    private List<SectionBean> mData = null;

    private int mContentId = -1;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle arg = getArguments();

        if (arg != null) {
            mContentId = arg.getInt(ARG_CONTENT_ID);
        }
    }

    public static ContentDelegate newInstance(int contentID) {

        Bundle arg = new Bundle();

        arg.putInt(ARG_CONTENT_ID, contentID);

        ContentDelegate delegate = new ContentDelegate();

        delegate.setArguments(arg);

        return delegate;

    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_vertical_content;
    }

    private void initData() {

        RestClient.builder()
                .loader(getContext())
                .url("sort_content_list.php?contentId="+mContentId)
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {

                        mData = new SectionDataConverter().convert(response);

                        SectionAdapter adapter = new SectionAdapter(R.layout.item_section_content,R.layout.item_section_header,mData);

                        mRecyclerView.setAdapter(adapter);

                    }
                })
                .build()
                .get();
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {

        StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);

        mRecyclerView.setLayoutManager(manager);

        initData();
    }


}
